package AIEngine;

import java.io.File;
import java.io.IOException;

import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.Number;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

public class Statistics {
	
	private WritableWorkbook wworkbook;
	private WritableSheet wsheet;
	private File file;
	
	public Statistics() throws IOException, RowsExceededException, WriteException {
		file = new File("statistics.xls");
		wworkbook = Workbook.createWorkbook(file);
		wsheet = wworkbook.createSheet("First Sheet", 0);
		Label MMvsMM = new Label(2, 2, "MinMax vs MinMax");
		Label MMvsAB = new Label(5, 2, "MinMax vs AlphaBeta");
		Label ABvsAB = new Label(8, 2, "AlphaBeta vs AlphaBeta");
		Label l1 = new Label(2,3, "0");
		Label l2 = new Label(2,4, "0");
		Label l3 = new Label(2,5, "0");
		Label l4 = new Label(5,3, "0");
		Label l5 = new Label(5,4, "0");
		Label l6 = new Label(5,5, "0");
		Label l7 = new Label(8,3, "0");
		Label l8 = new Label(8,4, "0");
		Label l9 = new Label(8,5, "0");
		wsheet.addCell(MMvsMM);
		wsheet.addCell(MMvsAB);
		wsheet.addCell(ABvsAB);
		wsheet.addCell(l1);
		wsheet.addCell(l2);
		wsheet.addCell(l3);
		wsheet.addCell(l4);
		wsheet.addCell(l5);
		wsheet.addCell(l6);
		wsheet.addCell(l7);
		wsheet.addCell(l8);
		wsheet.addCell(l9);
		wworkbook.write();
		wworkbook.close();
	}
	
	public void addResults(int firstPlayer, int secondPlayer, int winner) throws RowsExceededException, WriteException, IOException, BiffException{ 
		if(winner == 1){ //il primo vince
			File oldFile = file;
			Workbook oldWorkbook = Workbook.getWorkbook(oldFile);
			Sheet oldWSheet = oldWorkbook.getSheet("First Sheet");
			String string = oldWSheet.getCell(2, 3).getContents();
		    int oldValue = Integer.parseInt(string);
		    Number number = new Number(2, 3, oldValue+1);
		    oldFile.delete();
		    
		    updateFile(oldWorkbook, number);
		}
		else if(winner == 2){ // il secondo vince
			File oldFile = file;
			Workbook oldWorkbook = Workbook.getWorkbook(oldFile);
			Sheet oldWSheet = oldWorkbook.getSheet("First Sheet");
			String string = oldWSheet.getCell(2, 4).getContents();
		    int oldValue = Integer.parseInt(string);
		    Number number = new Number(2, 4, oldValue+1);
		    oldFile.delete();
		    
		    updateFile(oldWorkbook, number);
		}
		else { //pareggio
			File oldFile = file;
			Workbook oldWorkbook = Workbook.getWorkbook(oldFile);
			Sheet oldWSheet = oldWorkbook.getSheet("First Sheet");
			String string = oldWSheet.getCell(2, 5).getContents();
		    int oldValue = Integer.parseInt(string);
		    Number number = new Number(2, 5, oldValue+1);
		    oldFile.delete();
		    
		    updateFile(oldWorkbook, number);
		}
	}
	
	public void addResults(int firstPlayer, int winner)throws RowsExceededException, WriteException, IOException, BiffException{ 
		if(winner == 1){ //il primo vince
			File oldFile = file;
			Workbook oldWorkbook = Workbook.getWorkbook(oldFile);
			Sheet oldWSheet = oldWorkbook.getSheet("First Sheet");
			String string = oldWSheet.getCell(5, 3).getContents();
		    int oldValue = Integer.parseInt(string);
		    Number number = new Number(5, 3, oldValue+1);
		    oldFile.delete();
		    
		    updateFile(oldWorkbook, number);
		}
		else if(winner == 2){ // il secondo vince
			File oldFile = file;
			Workbook oldWorkbook = Workbook.getWorkbook(oldFile);
			Sheet oldWSheet = oldWorkbook.getSheet("First Sheet");
			String string = oldWSheet.getCell(5, 4).getContents();
		    int oldValue = Integer.parseInt(string);
		    Number number = new Number(5, 4, oldValue+1);
		    oldFile.delete();
		    
		    updateFile(oldWorkbook, number);
		}
		else { //pareggio
			File oldFile = file;
			Workbook oldWorkbook = Workbook.getWorkbook(oldFile);
			Sheet oldWSheet = oldWorkbook.getSheet("First Sheet");
			String string = oldWSheet.getCell(5, 5).getContents();
		    int oldValue = Integer.parseInt(string);
		    Number number = new Number(5, 5, oldValue+1);
		    oldFile.delete();
		    
		    updateFile(oldWorkbook, number);
		}
	}
	
	public void addResults(int winner)throws BiffException, IOException, RowsExceededException, WriteException{
		if(winner == 1){ //il primo vince
			File oldFile = file;
			Workbook oldWorkbook = Workbook.getWorkbook(oldFile);
			Sheet oldWSheet = oldWorkbook.getSheet("First Sheet");
			String string = oldWSheet.getCell(8, 3).getContents();
		    int oldValue = Integer.parseInt(string);
		    Number number = new Number(8, 3, oldValue+1);
		    oldFile.delete();
		    
		    updateFile(oldWorkbook, number);
		}
		else if(winner == 2){ // il secondo vince
			File oldFile = file;
			Workbook oldWorkbook = Workbook.getWorkbook(oldFile);
			Sheet oldWSheet = oldWorkbook.getSheet("First Sheet");
			String string = oldWSheet.getCell(8, 4).getContents();
		    int oldValue = Integer.parseInt(string);
		    Number number = new Number(8, 4, oldValue+1);
		    oldFile.delete();
		    
		    updateFile(oldWorkbook, number);
		}
		else { //pareggio
			File oldFile = file;
			Workbook oldWorkbook = Workbook.getWorkbook(oldFile);
			Sheet oldWSheet = oldWorkbook.getSheet("First Sheet");
			String string = oldWSheet.getCell(8, 5).getContents();
		    int oldValue = Integer.parseInt(string);
		    Number number = new Number(8, 5, oldValue+1);
		    oldFile.delete();
		    
		    updateFile(oldWorkbook, number);
		}
	}
	
	private void updateFile(Workbook oldWorkbook, Number number) throws IOException, RowsExceededException, WriteException{
		File newFile = new File("statistics.xls");
	    file = newFile;
	    WritableWorkbook wworkbook=Workbook.createWorkbook(file, oldWorkbook);
	    wsheet = wworkbook.getSheet("First Sheet");
	    wsheet.addCell(number);
	    wworkbook.write();
	    wworkbook.close();
	}

}



